package com.arcvideo.acsplayer.ui;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;
import com.arcvideo.acsplayer.mode.LocalVideoInfo;

public class LoadArclouldChildListActivity extends Activity implements OnClickListener, OnItemClickListener {
	private TextView mTitleText = null;
	private ImageButton mBackButton = null;

	private ListView mVideoListView = null;
	private ArrayList<LocalVideoInfo> mVideoList = null;
	private VideoAdapter mVideoAdapter = null;
	private String mUrl = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_load_clould_list);
		initUi();
		initData();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.id_title_left_btn :
				finish();
				break;
			default :
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		// mId = mVideoList.get(position).getPath();
		toPlayActivity();
	}

	/**
	 * 初始化UI
	 */
	private void initUi() {
		mTitleText = (TextView) findViewById(R.id.id_title_text_view);
		mTitleText.setText(R.string.select_video_from_clould);

		mBackButton = (ImageButton) findViewById(R.id.id_title_left_btn);
		mBackButton.setOnClickListener(this);

		mVideoListView = (ListView) findViewById(R.id.list);
		mVideoListView.setOnItemClickListener(this);
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		test();
	}

	private void test() {
		mVideoList = new ArrayList<LocalVideoInfo>();
		LocalVideoInfo info1 = new LocalVideoInfo();
		info1.setDisplayName("11");
		mVideoList.add(info1);

		mVideoAdapter = new VideoAdapter(getApplicationContext(), mVideoList);
		mVideoListView.setAdapter(mVideoAdapter);
	}

	/**
	 * 进入播放页面
	 */
	private void toPlayActivity() {
		Intent displayIntent = new Intent(LoadArclouldChildListActivity.this, PlayActivity.class);
		displayIntent.putExtra("URL", mUrl);
		startActivity(displayIntent);
	}

	public class VideoAdapter extends BaseAdapter {
		/**
		 * 视频信息
		 */
		private ArrayList<LocalVideoInfo> videoList = new ArrayList<LocalVideoInfo>();

		/**
		 * 上下文
		 */
		private Context context;

		/**
		 * 构造函数
		 */
		public VideoAdapter(Context context, ArrayList<LocalVideoInfo> videoList) {
			this.context = context;
			this.videoList = videoList;
		}

		/**
		 * 更新列表
		 */
		public void updateList(ArrayList<LocalVideoInfo> videoList) {
			this.videoList = videoList;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return this.videoList.size();
		}

		@Override
		public Object getItem(int position) {
			return this.videoList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("NewApi")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			final LocalVideoInfo video = this.videoList.get(position);
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.item_arcclould_file, null);
				viewHolder.video_title = (TextView) convertView.findViewById(R.id.video_file_title);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.video_title.setText(video.getDisplayName());
			return convertView;
		}

	}

	final static class ViewHolder {
		TextView video_title;
	}

}